#!/bin/bash
################################################################################
#  Copyright (C) 2025 IQIYI All rights reserved.
#
#  Author     : 王海斌
#  E-mail     : wanghaibin@qiyi.com
#  Version    :
#  Date       : Tue 22 Apr 2025 10:52:30 AM CST
#  Destription:  按行统计文件中每行的字节个数,按照256/512/1024等区间段进行统计
#
################################################################################
cd `dirname $0`

#  是否显示执行过程
#set -x 

# 检查是否传入文件名参数
if [ "$#" -ne 1 ]; then
  echo "Usage: $0 <filename>"
  exit 1
fi

# 获取文件名参数
file="$1"

# 用awk计算每行字节数，将结果存储在数组中
line_lengths=($(awk '{ print length($0) }' "$file"))

# 定义区间大小
intervals=(256 512 1024 2048 4096 8192)

# 使用关联数组统计区间数量
declare -A counts

# 初始化统计表
for i in "${intervals[@]}"; do
  counts["$i"]=0
done

# 统计每行的字节数，并进行区间分类
for length in "${line_lengths[@]}"; do
  for i in "${!intervals[@]}"; do
    if (( length <= intervals[i] )); then
      ((counts["${intervals[i]}"]++))
      break
    fi
  done
done

# 打印每个区间的统计结果
for i in "${!intervals[@]}"; do
  if (( i == 0 )); then
    echo "0 - ${intervals[i]} bytes: ${counts[${intervals[i]}]}"
  else
    echo "$((intervals[i-1] + 1)) - ${intervals[i]} bytes: ${counts[${intervals[i]}]}"
  fi
done

